from fastapi import Request
from fastapi import APIRouter
from app.handler.gdb import GdbPromqtManager
from app.handler.struct_scheme import MysqlPromqtManager
from app.handler.struct_es import ElasticiSearchPromqtManager
from app.response.base_response import response_ok
from app.utils.data_parse import parser_gdb_data, parser_mysql_data
from app.model.request_model import QuestionModel

routers = APIRouter()


@routers.post("/generate/gdb", description='根据问题生成GDB的查询语句', tags=['根据问题生成GDB的查询语句'])
async def generate_gdb(request: Request, q: QuestionModel):
    chain = GdbPromqtManager(request).get_chat_handler()
    return parser_gdb_data({
        'question': q.question,
        'answer': chain.invoke({'question': q.question}).content or ''
    })


@routers.post("/generate/mysql", description='根据问题生成MYSQL的查询语句', tags=['根据问题生成MYSQL的查询语句'])
async def generate_mysql(request: Request, q: QuestionModel):
    return parser_mysql_data({
        'question': q.question,
        'answer': MysqlPromqtManager().get_mysql_handler(q.question)
    })


@routers.post("/generate/es", description='根据问题生成ES的查询语句', tags=['根据问题生成ES的查询语句'])
async def generate_es(request: Request, q: QuestionModel):
    return response_ok({
        'question': q.question,
        'answer': ElasticiSearchPromqtManager(request).get_es_handler(q.question)
    })
